package webdev.adapters;
import java.util.ArrayList;

import webdev.imgloader.ImageLoader;
import webdev.models.Location;
import webdev.utils.PhysicalScreenUtil;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import dev.danang.R;


public class ListLocationAdapter extends BaseAdapter{
	
	Context context;
	LayoutInflater inflater;
	ImageLoader imgLoader;
	public ArrayList<Location> arr = new ArrayList<Location>();
	int vwidth=0;
	public ListLocationAdapter(Context context)
	{
		this.context = context;
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imgLoader = new ImageLoader(context);
		vwidth = PhysicalScreenUtil.getWidth(context);
	}
	@Override
	public int getCount() {
		return arr.size();
	}
	public void addItem(Location item){
		arr.add(item);
		notifyDataSetChanged();
	}
	@Override
	public Object getItem(int position) {
		return arr.get(position);
	}
	@Override
	public long getItemId(int position) {
		return 0;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		View view = convertView;
		ViewHolder viewHolder = null;
		Location item = arr.get(position);
		if(view==null)
		{
			viewHolder 					= new ViewHolder();
			convertView 				= inflater.inflate(R.layout.location_list_item, null);
			viewHolder.imageLocation 		= (ImageView) convertView.findViewById(R.id.img_Location);
			viewHolder.ratingLocation 	= (RatingBar) convertView.findViewById(R.id.img_starLocation);
			viewHolder.nameLocation 			= (TextView) convertView.findViewById(R.id.tvt_Location);
			viewHolder.addressLocation 		= (TextView) convertView.findViewById(R.id.tvt_AdressLocation);
			viewHolder.progresslocation 		= (ProgressBar)convertView.findViewById(R.id.progress);
			
		}
		
		
		try
		{
			int width = 80;
			int height = 60;
			if(width < 480){
				width = 80;
				height = 60;
			}
			else{
				width = 120;
				height = 85;
			}
			viewHolder.imageLocation.getLayoutParams().width = width;
			viewHolder.imageLocation.getLayoutParams().height = height;
			viewHolder.nameLocation.setText(item.name);
			
			viewHolder.addressLocation.setText(item.getAddress());
			float rate = 0; 
			try {
				rate = Float.parseFloat(item.rate);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			viewHolder.ratingLocation.setRating(rate);
			
			if(vwidth < 480){
				viewHolder.imageLocation.getLayoutParams().width = 80;
				viewHolder.imageLocation.getLayoutParams().height = 60;
			}
			else{
				viewHolder.imageLocation.getLayoutParams().width = 120;
				viewHolder.imageLocation.getLayoutParams().height = 85;
			}
			if((item.urlImage == null) || item.urlImage.equals("")){
				viewHolder.imageLocation.setVisibility(View.VISIBLE);
				viewHolder.progresslocation.setVisibility(View.GONE);
				viewHolder.imageLocation.setImageResource(R.drawable.color12);
			}
			else
				imgLoader.DisplayImage(item.urlImage, viewHolder.imageLocation, true,width,height);
			
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}
	public void removeAll(){
		arr.clear();
		notifyDataSetChanged();
	}
	public void remove(Location itemFavourites)
	{
		arr.remove(itemFavourites);
		notifyDataSetChanged();
	}
	class ViewHolder
	{
		public TextView nameLocation;
	    public TextView addressLocation;
	    public ImageView imageLocation;
	    public RatingBar ratingLocation;
	    public ProgressBar progresslocation;
	}
}